﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IBll;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Chinaer.Core.BaseCore.Extensions;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class Login : Repository, ILogin
    {

        public DTO.ShangJiaDTO LoginShangJia(DTO.ShangJiaDTO shangjiaDTO)
        {
            IDao.IShangJiaDAO shangJiaDao = new Chinaer.Dao.ShangJiaDAO();
            if (shangjiaDTO == null)
            {
                return new DTO.ShangJiaDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull
                };
            }
            //密码为空
            if (shangjiaDTO.LoginPassword.CheckIsNull().Success == false)
            {
                return new DTO.ShangJiaDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull
                };
            }


            //判断用户名是否存在
            if (shangJiaDao.GetByName(shangjiaDTO.LoginName) == null)
            {
                return new DTO.ShangJiaDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNotExists
                };
            }
            //用户名为空
            if (shangjiaDTO.LoginName.CheckIsNull().Success == false)
            {
                return new DTO.ShangJiaDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull
                };
            }

            DTO.ShangJia_CaoZuoDTO shangjiaDTO1 = new DTO.ShangJia_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "LoginShangJia",
                CaoZuoTime = DateTime.Now,
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                UserID = shangjiaDTO.ID,
                UserName = shangjiaDTO.LoginName
            };
            Bll.ShangJiaCaoZuo.Instance.Add(shangjiaDTO1);

            string loginName = shangjiaDTO.LoginName;
            string loginPassword = shangjiaDTO.LoginPassword;
            string sql = "select count(*) from shangjia where loginName=@loginName and LoginPassword=@loginPassword";
            SqlParameter paraUserName = new SqlParameter("@loginName", SqlDbType.NVarChar, 50);
            SqlParameter paraPassword = new SqlParameter("@loginPassword", SqlDbType.NVarChar, 50);
            paraUserName.SqlValue = loginName;
            paraPassword.SqlValue = loginPassword;
            System.Data.SqlClient.SqlParameter[] parameters = new System.Data.SqlClient.SqlParameter[] {  
                paraUserName,
                paraPassword
            };
            Chinaer.Core.Helper.LogHelper.Instance.WriteSQLLog(sql);
            int result = Convert.ToInt32(Chinaer.Core.Helper.SqlHelper.ExecuteScalarNonConn(CommandType.Text, sql, parameters).ToString());
            Chinaer.DTO.ShangJiaDTO shangJiaDTOResult = new DTO.ShangJiaDTO();
            //表示用户存在
            if (result >= 1)
            {
                Model.Entities.Model_ShangJia model_shangJia = shangJiaDao.GetByName(loginName);
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_shangJia, shangJiaDTOResult);
                shangJiaDTOResult.ApiToken = Chinaer.Core.Helper.GenerateTokenIDHelper.GenerateToken(model_shangJia.ID);
                shangJiaDTOResult.LoginStatus = Core.BaseCore.LoginStatus.Success;

                IDianPu dianPu = new Bll.DianPu();
                DTO.DianPuDTO dianPuDTO = dianPu.GetDianPuByShangJiaID(shangjiaDTO.ID);

                if (dianPuDTO != null)
                {
                    shangJiaDTOResult.DianPuID = dianPuDTO.ID;
                }
                else
                {
                    shangJiaDTOResult.DianPuID = null;
                }

                bool cacheFlag = Cache.CacheHelper.Memcached_Set(shangJiaDTOResult.ApiToken, shangJiaDTOResult.ApiToken, Cache.CacheHelper.DueTime);
            }
            else
            {
                //表示登录失败
                shangJiaDTOResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.WrongPasswordOrUserName;
            }

            IBll.IShangJiaLoginDetail shangJiaLoginDetail = new Bll.ShangJiaLoginDetail();
            shangJiaLoginDetail.Add(new DTO.ShangJiaLoginDetailDTO()
            {
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                LoginName = shangjiaDTO.LoginName,
                LoginStatus = shangJiaDTOResult.LoginStatus
            });

            return shangJiaDTOResult;
        }

        public DTO.AdministratorDTO LoginAdministrator(DTO.AdministratorDTO administratorDTO)
        {
            if (administratorDTO == null)
            {
                return new DTO.AdministratorDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull
                };
            }

            //判断用户名或密码是否为空
            if (administratorDTO.LoginName.CheckIsNull().Success == false)
            {
                return new DTO.AdministratorDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull
                };
            }
            if (administratorDTO.LoginPassword.CheckIsNull().Success == false)
            {
                return new DTO.AdministratorDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull
                };
            }
            IDao.IAdministratorDAO administratorDAO = new Dao.AdministratorDAO();
            //判断登录用户是否存在
            if (administratorDAO.GetByName(administratorDTO.LoginName) == null)
            {
                return new DTO.AdministratorDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNotExists
                };
            }

            Bll.AdministratorCaoZuo.Instance.Add(new DTO.Administrator_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "LoginAdministrator",
                CaoZuoTime = DateTime.Now,
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                UserID = administratorDTO.ID,
                UserName = administratorDTO.LoginName
            });


            Model.Entities.Model_Administrator model_Administrator = new Model.Entities.Model_Administrator();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(administratorDTO, model_Administrator);

            DTO.AdministratorDTO administratorDTOResult = new DTO.AdministratorDTO();
            var result = administratorDAO.GetByNameAndPassword(administratorDTO.LoginName, administratorDTO.LoginPassword);
            if (result == null)
            {
                administratorDTOResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.WrongPasswordOrUserName;
            }
            else
            {
                model_Administrator = administratorDAO.GetByName(administratorDTO.LoginName);
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Administrator, administratorDTOResult);
                administratorDTOResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Success;
            }


            //添加到登陆详情表中
            IBll.IAdministratorLoginDetail administratorLoginDetail = new Bll.AdministratorLoginDetail();
            administratorLoginDetail.Add(new DTO.AdministratorLoginDetailDTO()
            {
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                LoginName = administratorDTO.LoginName,
                LoginStatus = administratorDTOResult.LoginStatus
            });
            return administratorDTOResult;
        }

        public DTO.CustomerDTO LoginCustomer(DTO.CustomerDTO customerDTO)
        {
            //判断传入的实体是否为空
            if (customerDTO == null)
            {
                return new DTO.CustomerDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull

                };
            }

            ///判断用户名或密码是否存在
            if (customerDTO.LoginName.CheckIsNull().Success == false)
            {
                return new DTO.CustomerDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull//表示用户名已经存在
                };
            }
            if (customerDTO.LoginPassword.CheckIsNull().Success == false)
            {
                return new DTO.CustomerDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull//表示用户名已经存在
                };
            }

            //判断用户名是否存在
            IDao.ICustomerDAO customerDAO = new Dao.CustomerDAO();
            //判断用户是否存在
            if (customerDAO.GetByName(customerDTO.LoginName) == null)
            {
                return new DTO.CustomerDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNotExists//表示用户名已经存在
                };
            }

            Bll.CustomerCaoZuo.Instance.Add(new DTO.Customer_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "LoginCustomer",
                CaoZuoTime = DateTime.Now,
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                UserName = customerDTO.LoginName
            });

            Model.Entities.Model_Customer model_Customer = new Model.Entities.Model_Customer();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(customerDTO, model_Customer);

            DTO.CustomerDTO customerDTOResult = new DTO.CustomerDTO();
            var result = customerDAO.GetByNameAndPassword(customerDTO.LoginName, customerDTO.LoginPassword);
            if (result == null)
            {
                customerDTOResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.WrongPasswordOrUserName;
            }
            else
            {
                model_Customer = customerDAO.GetByName(customerDTO.LoginName);
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Customer, customerDTOResult);
                customerDTOResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Success;
            }

            //添加到登陆详情表中
            IBll.ICustomerLoginDetail customerLoginDetail = new Bll.CustomerLoginDetail();
            customerLoginDetail.Add(new DTO.CustomerLoginDetailDTO()
            {
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                LoginName = customerDTO.LoginName,
                LoginStatus = customerDTOResult.LoginStatus
            });
            return customerDTOResult;
        }

        public DTO.EmployeeDTO LoginEmployee(DTO.EmployeeDTO employeeDTO)
        {
            if (employeeDTO == null)
            {
                return new DTO.EmployeeDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull
                };
            }
            if (employeeDTO.LoginName.CheckIsNull().Success == false)
            {
                return new DTO.EmployeeDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull
                };
            }
            if (employeeDTO.LoginPassword.CheckIsNull().Success == false)
            {
                return new DTO.EmployeeDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull
                };
            }
            IDao.IEmployeeDAO employeeDAO = new Dao.EmployeeDAO();
            if (employeeDAO.GetByName(employeeDTO.LoginName) == null)
            {
                return new DTO.EmployeeDTO()
                {
                    LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNotExists//用户名存在
                };
            }

            Bll.EmployeeCaoZuo.Instance.Add(new DTO.Employee_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "LoginEmployee",
                CaoZuoTime = DateTime.Now,
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                UserID = employeeDTO.ID,
                UserName = employeeDTO.LoginName
            });

            Model.Entities.Model_Employee model_Employee = new Model.Entities.Model_Employee();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(employeeDTO, model_Employee);

            DTO.EmployeeDTO employeeDTOResult = new DTO.EmployeeDTO();

            var model_employee = employeeDAO.GetByNameAndPassword(employeeDTO.LoginName, employeeDTO.LoginPassword);
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_employee, employeeDTOResult);

            if (employeeDTOResult != null)
            {
                model_Employee = employeeDAO.GetByName(employeeDTO.LoginName);
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Employee, employeeDTOResult);
                employeeDTOResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Success;
            }
            else
            {
                employeeDTOResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.WrongPasswordOrUserName;
            }

            //添加到登陆详情表中
            IBll.IEmployeeLoginDetail employeeLoginDetail = new Bll.EmployeeLoginDetail();
            employeeLoginDetail.Add(new DTO.EmployeeLoginDetailDTO()
            {
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                LoginName = employeeDTO.LoginName,
                LoginStatus = employeeDTOResult.LoginStatus
            });
            return employeeDTOResult;
        }
    }
}
